package net.pm.core.web.power;

import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.PMTool;
import net.pm.core.cache.PMPowerCache;
import net.pm.core.cache.SessionStorer;
import net.pm.core.entities.db.Pmdept;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.service.pmuser.PMDeptServicer;
import net.pm.core.service.pmuser.PMUserServicer;
import net.pm.core.service.power.DeptPowerServicer;
import net.pm.core.service.power.UserPowerServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/power/")
public class PowerController extends BaseController {

	@Resource(name = "UserPowerServicer")
	private UserPowerServicer userPowerServicer;
	@Resource(name = "DeptPowerServicer")
	private DeptPowerServicer deptPowerServicer;
	@Resource(name = "PMUserServicer")
	private PMUserServicer userServicer;
	@Resource(name = "PMDeptServicer")
	private PMDeptServicer deptServicer;

	@RequestMapping(value = "user/distribution", method = RequestMethod.GET)
	public ModelAndView distributionUserPower(Long userId) {
		ModelAndView mav = new ModelAndView(
				"assist/power/power_distribution_userpower");
		mav.addObject("userId", userId);
		mav.addObject("powers", PMPowerCache.getPmpowerset());
		mav.addObject("userpowers",
				userPowerServicer.findUserPowerCodesByUserId(userId));
		mav.addObject("PMTool", super.useStaticPacker(PMTool.class.getName()));
		return mav;
	}

	/**
	 * 给用户分配权限　
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "user/distribution", method = RequestMethod.PUT)
	public String distributionUserPower(Long userId, Integer[] powerIds) {
		long result = userPowerServicer.modifyUserPower(userId, powerIds);
		if (result == UserPowerServicer.power_modify_success) {
			Pmuser pmuser = userServicer.findEntityById(userId);

			HttpSession session = SessionStorer.fetch(pmuser.getPmdept()
					.getId(), userId);
			if (PMTool.isNotEmpty(session)) {
				session.setAttribute(DevConstant.SESSION_CURRENT_USER_POWER,
						userPowerServicer.findUserPowerCodesByUserId(userId));
			}
			super.setSessionMessage("更新权限成功!");
		} else {
			super.setSessionMessage("更新权限失败!");
		}
		return "redirect:/user/manage.htm";
	}

	@RequestMapping(value = "dept/distribution", method = RequestMethod.GET)
	public ModelAndView distributionDeptPower(Integer deptId) {
		ModelAndView mav = new ModelAndView(
				"assist/power/power_distribution_deptpower");
		mav.addObject("deptId", deptId);
		mav.addObject("powers", PMPowerCache.getPmpowerset());
		mav.addObject("deptpowers",
				deptPowerServicer.findDeptPowerCodesByDeptId(deptId));
		mav.addObject("PMTool", super.useStaticPacker(PMTool.class.getName()));
		return mav;
	}
	
	/**
	 * 给部门分配权限
	 * 
	 * @param deptId
	 * @param powerIds
	 * @return
	 */
	@RequestMapping(value = "dept/distribution", method = RequestMethod.PUT)
	public String distributionDeptPower(Integer deptId, Integer[] powerIds) {
		long result = deptPowerServicer.modifyDeptPower(deptId, powerIds);
		if (result == DeptPowerServicer.power_modify_success) {
			Pmdept pmdept = deptServicer.findEntityById(deptId);

			Set<Pmuser> pmusers = pmdept.getPmusers();
			for (Pmuser user : pmusers) {
				HttpSession session = SessionStorer.fetch(deptId, user.getId());
				if (PMTool.isNotEmpty(session)) {
					session.setAttribute(DevConstant.SESSION_CURRENT_DEPT_POWER,
							deptPowerServicer.findDeptPowerCodesByDeptId(deptId));
				}
			}

			super.setSessionMessage("更新权限成功!");
		} else {
			super.setSessionMessage("更新权限失败!");
		}
		return "redirect:/dept/info.htm";
	}

}
